//
//  SSFirstPageDiscountView.m
//  Travel_World
//
//  Created by dlios on 15/10/13.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "SSFirstPageDiscountView.h"
#import "SSFirstPageDiscountModel.h"
#import "QTAFNnetWorkTool.h"
#import "UIImageView+WebCache.h"
#import "SSFirstPageDiscountMainCollectionViewcell.h"
#import "SSFirstPageWebView.h"
#import "SSFirstPageDiscountTitleView.h"
#import "Masonry.h"
#import "SSCollectionModel.h"
#import "SSFirstPageDiscountTitleModel.h"

@interface SSFirstPageDiscountView ()<UICollectionViewDelegate, UICollectionViewDataSource, SSFirstPageDiscountTitleViewDelegate>

@property (nonatomic, copy) NSString *myURL;
@property (nonatomic, strong) SSFirstPageDiscountTitleView *titleButtonView;
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic, strong) NSMutableArray *discountArr;

@end

@implementation SSFirstPageDiscountView


- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.myURL = @"http://open.qyer.com/lastminute/app_lastminute_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=0&departure=&is_show_pay=1&is_show_supplier=1&lat=38.8826676005059&lon=121.5397836874259&max_id=0&oauth_token=3ccf6be2c77dee4d33e6d6a5eb878fc7&page=1&page_size=20&product_type=0&times=";
    }
    
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"折扣";
    
    self.titleButtonView = [[SSFirstPageDiscountTitleView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) delegate:self];
    
    [self.view addSubview:self.titleButtonView];
    
    [self.titleButtonView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.top.bottom.equalTo(self.view);
    }];
    
    [self createLeft];
    [self createMainCollectionView];
    [self createData:self.myURL];

}


- (NSString *)urlForm
{
    return @"http://open.qyer.com/lastminute/get_all_categorys";
}


- (void)createLeft
{
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"<< 首页" style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonClick)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)leftButtonClick
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)createMainCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((MINW - 30 * W) / 2, 200 * H);
    flowLayout.minimumInteritemSpacing = 9 * W;
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * H, 10 * W, 10 * H, 10 * W);
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    [self.titleButtonView addSubview:self.mainCollectionView];
    
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    [self.mainCollectionView registerClass:[SSFirstPageDiscountMainCollectionViewcell class] forCellWithReuseIdentifier:@"discount"];
    
    [self.mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.and.right.bottom.equalTo(self.titleButtonView);
        make.top.equalTo(self.titleButtonView.mas_top).offset(35);
        
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.discountArr.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SSFirstPageDiscountMainCollectionViewcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"discount" forIndexPath:indexPath];
    
    SSFirstPageDiscountModel *discountModel = self.discountArr[indexPath.row];
    
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:discountModel.pic] placeholderImage:[UIImage imageNamed:@"placeholder1"]];
    cell.titleLabel.text = discountModel.title;
    [cell.titleLabel sizeToFit];
    cell.timeLabel.text = discountModel.departureTime;
    cell.lastminute_des.text = discountModel.lastminute_des;
    
    NSMutableAttributedString *priceStr=[[NSMutableAttributedString alloc ]initWithData:[discountModel.price dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    
    NSRange range  = NSMakeRange(0, priceStr.length - 2);
    
    [priceStr setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:16]} range:range];
    cell.priceLabel.attributedText = priceStr;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SSCollectionModel *collectionModel = [[SSCollectionModel alloc] init];
    
    SSFirstPageWebView *firstPageWebView = [[SSFirstPageWebView alloc] init];
    if (self.discountArr.count > indexPath.row) {
        
        SSFirstPageDiscountModel *discountModel = self.discountArr[indexPath.row];
        firstPageWebView.myURL = [NSString stringWithFormat:@"http://m.qyer.com/z/deal/%@/?source=app&client_id=qyer_ios&track_app_version=6.5.5&track_deviceid=5B302CA5-E537-4B10-955D-520F4C91BAFE&track_user_id=6654242",discountModel.myid];
        
        collectionModel.title = discountModel.title;
        collectionModel.pic = discountModel.pic;
        collectionModel.price = discountModel.price;
        collectionModel.url = discountModel.url;
        
        firstPageWebView.collectionModel = collectionModel;
    }
    
    [self.navigationController pushViewController:firstPageWebView animated:YES];
}

- (void)createData:(NSString *)url
{
    [QTAFNnetWorkTool getUrl:url body:nil hudSuperView:self.titleButtonView result:QTJSON headerFile:nil success:^(id result) {
        self.discountArr = [SSFirstPageDiscountModel baseModelByArr:result[@"data"][@"lastminutes"]];
        
        if (self.discountArr.count == 0) {
            
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有相关内容" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
            
            [alertController addAction:action];
            [self presentViewController:alertController animated:YES completion:nil];
        }
        
        [self.mainCollectionView reloadData];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
